package com.webinsky.publish.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.publish.persist.entity.EmailSetting;


public interface EmailSettingMapper {
	@Insert({
		"insert into `email_setting` (site_id, smtp, smtp_port, sender_address, sender_password, reciever_addresses, create_at)",
		"values (#{siteId, jdbcType=INTEGER}, #{smtp,jdbcType=VARCHAR}, #{port,jdbcType=INTEGER}, #{sender,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{recievers,jdbcType=VARCHAR}, now())"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(EmailSetting record);
	
	@Select({
			"select id, site_id, smtp, smtp_port, sender_address, sender_password, reciever_addresses, create_at",
			"from `email_setting`",
			"where id = #{id, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="smtp", property="smtp", jdbcType=JdbcType.VARCHAR),
	    @Result(column="smtp_port", property="port", jdbcType=JdbcType.INTEGER),
	    @Result(column="sender_address", property="sender", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sender_password", property="password", jdbcType=JdbcType.VARCHAR),
	    @Result(column="reciever_addresses", property="recievers", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP)
	})
	EmailSetting selectById(@Param("id")int id);
	
	@Select({
		"select id, site_id, smtp, smtp_port, sender_address, sender_password, reciever_addresses, create_at",
		"from `email_setting`",
		"where site_id = #{siteId, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="smtp", property="smtp", jdbcType=JdbcType.VARCHAR),
	    @Result(column="smtp_port", property="port", jdbcType=JdbcType.INTEGER),
	    @Result(column="sender_address", property="sender", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sender_password", property="password", jdbcType=JdbcType.VARCHAR),
	    @Result(column="reciever_addresses", property="recievers", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP)
	})
	List<EmailSetting> selectBySiteId(@Param("siteId")int siteId);
	
	@Update({
       "update `email_setting`",
	       " set smtp = #{smtp,jdbcType=VARCHAR},",
	       " smtp_port = #{port, jdbcType=INTEGER},",
	       " sender_address = #{sender, jdbcType=VARCHAR},",
	       " sender_password = #{password,jdbcType=VARCHAR},",
	       " reciever_addresses = #{recievers,jdbcType=VARCHAR} ",
       "where Id = #{id,jdbcType=INTEGER}"
	   })
	   int updateByPrimaryKey(EmailSetting record);	
}
